﻿using Anke.SHManage.BLL;
using Anke.SHManage.Model;
using Anke.SHManage.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Anke.SHManage.Web.Areas.CM.Controllers
{
    public class CardInfoController : Controller
    {
        private static object m_SyncRoot = new Object();//互斥对象
        //
        // GET: /CM/CardInfo/
        //油卡基础信息主页面
        public ActionResult CardInfoList()
        {
            E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();
            this.ViewData["em"] = em;
            string selfWorkCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
            this.ViewData["selfWorkCode"] = selfWorkCode;
            string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;
            this.ViewData["selfCenterID"] = selfCenterID;
            string selfStationID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID;
            this.ViewData["selfStationID"] = selfStationID;
            this.ViewData["name"] = UserOperateContext.Current.Session_UsrInfo.Name;
            this.ViewData["time"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            UserOperateContext userOperateContext = new UserOperateContext();
            bool Insert = userOperateContext.getGongneng(1258);//判断登录人有“新增”油卡信息权限
            if (Insert)
            {
                this.ViewData["RolePermissionInsert"] = "Insert";//登录人有新增油卡信息权限
            }
            return View();
        }

        /// <summary>
        /// 查询列表信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DataLoad()
        {
            try
            {
                string isFanBan = Request.Form["isFanBan"];
                string isActive = Request.Form["isActive"];
                string cardCode = Request.Form["cardCode"];
                string driverWorkID = Request.Form["driverWorkID"];
                string driverName = Request.Form["driverName"];
                string center = Request.Form["center"];
                string station = Request.Form["station"];
                //获取页容量
                int pageIndex = int.Parse(Request.Form["rows"]);
                //获取请求的页码
                int pageSize = int.Parse(Request.Form["page"]);
                //根据登录人取工号所属分站，分中心编码，以及权限大小
                E_StatisticsPermisson em = UserOperateContext.Current.getMaxPerForStatistics();
                this.ViewData["em"] = em;
                string selfWorkCode = UserOperateContext.Current.Session_UsrInfo.WorkCode;
                this.ViewData["selfWorkCode"] = selfWorkCode;
                string selfCenterID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSubCenterID;
                this.ViewData["selfCenterID"] = selfCenterID;
                string selfStationID = UserOperateContext.Current.Session_UsrInfo.P_Department.DispatchSationID;
                this.ViewData["selfStationID"] = selfStationID;

                var list = new C_OilCardInfoBLL().GetCardList(pageIndex, pageSize, isFanBan, isActive, cardCode, driverWorkID,
                                                              driverName, center, station, em, selfWorkCode, selfCenterID, selfStationID);
                JsonResult j = this.Json(list, "appliction/json", JsonRequestBehavior.AllowGet);
                return j;
            }

            catch (Exception e)
            {
                Dictionary<string, string> dict = new Dictionary<string, string>();
                dict.Add("InfoID", "0");
                dict.Add("InfoMessage", e.Message);
                return this.Json(dict);
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CardInfoAdd()
        {
            string sDetailInfo = Request.Form["sCardInfo"].ToString();
            C_OilCardInfo info = JsonHelper.GetJsonInfoBy<C_OilCardInfo>(sDetailInfo);
            if (info.CenterID == null)
            { info.CenterID = 0; }
            if (info.StationID == null)
            { info.StationID = ""; }
            info.FillTime = DateTime.Now;
            info.LinkCardCode = info.CardCode;
            //卡内金额CardAmount字段，初次输入后不变，卡内余额bak1字段会根据充值和加油而改变
            info.bak1 = (info.CardAmount).ToString();
            List<C_OilCardInfo> list = new C_OilCardInfoBLL().GetMaxID();
            if (list.Count == 0)
            { info.ID = 1; }
            else
            { info.ID = list[0].ID + 1; }
            int res = new C_OilCardInfoBLL().Add(info);
            if (res > 0)
                return this.JsonResult(Utility.E_JsonResult.OK, "保存成功！", null, null);
            else
                return this.JsonResult(Utility.E_JsonResult.Error, "保存失败", null, null);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CardInfoEdit()
        {
            int id = Convert.ToInt32(Request.Form["id"]);
            string sDetailInfo = Request.Form["sCardInfo"].ToString();
            C_OilCardInfo info = JsonHelper.GetJsonInfoBy<C_OilCardInfo>(sDetailInfo);
            info.ID = id;
            if (info.CenterID == null)
            { info.CenterID = 0; }
            if (info.StationID == null)
            { info.StationID = ""; }
            info.FillTime = DateTime.Now;
            info.LinkCardCode = info.CardCode;
            int res = new C_OilCardInfoBLL().Modify(info, "CardCode", "DriverWorkID", "DriverName", "CardAmount", "IsActive", "CenterID",
                                                          "StationID", "Remark", "IsFanBan", "LinkCardCode", "FillTime", "FillPerson");
            if (res > 0)
                return this.JsonResult(Utility.E_JsonResult.OK, "保存成功！", null, null);
            else
                return this.JsonResult(Utility.E_JsonResult.Error, "保存失败", null, null);
        }

        /// <summary>
        /// 换卡、换人传ID
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CardInfo()
        {
            this.ViewData["ID"] = (Request.Form["id"]);
            string a = (Request.Form["id"]);
            bool state = Convert.ToBoolean(Request.Form["state"]);
            return View();
        }
        //换卡操作
        [HttpPost]
        public ActionResult CardInfoHK()
        {
            int id = Convert.ToInt32(Request.Form["id"]);
            //通过id取到原来油卡的全部信息
            C_OilCardInfo model = new C_OilCardInfoBLL().GetModelList(id);
            string sDetailInfo = Request.Form["sCardInfo"].ToString();
            C_OilCardInfo info = JsonHelper.GetJsonInfoBy<C_OilCardInfo>(sDetailInfo);
            //string centerID = new C_OilCardRechargeBLL().GetCenterID(model.CardCode);
            //string stationID = new C_OilCardRechargeBLL().GetStationID(model.CardCode);
            //info.CenterID = Convert.ToInt32(centerID);
            //info.StationID = stationID;
            info.CenterID = model.CenterID;
            info.StationID = model.StationID;
          
            //判断有无更改信息，如果无，则不做操作
            if (model.CardCode == info.CardCode && model.CardAmount == info.CardAmount && model.DriverName == info.DriverName && model.DriverWorkID == info.DriverWorkID)
            {
                return this.JsonResult(Utility.E_JsonResult.OK, "保存成功！", null, null);
            }
            //如果有，则按换卡操作流程走
            else
            {
                //先将原来卡的信息变为无效
                model.IsActive = false;
                //int res = new C_OilCardInfoBLL().Modify(model, "IsActive");
                //再新增换卡后信息
                info.FillTime = DateTime.Now;
                info.LinkCardCode = model.LinkCardCode;
                info.bak1 = (info.CardAmount).ToString();
                List<C_OilCardInfo> list = new C_OilCardInfoBLL().GetMaxID();
                if (list == null)
                { info.ID = 1; }
                else
                { info.ID = list[0].ID + 1; }
                int resh = new C_OilCardInfoBLL().Add(info);
                //换卡操作备注
                model.Remark = model.Remark + "(换卡操作,卡号换成" + info.CardCode + ")";
                int res = new C_OilCardInfoBLL().Modify(model, "IsActive", "Remark");
                if (resh > 0)
                    return this.JsonResult(Utility.E_JsonResult.OK, "保存成功！", null, null);
                else
                    return this.JsonResult(Utility.E_JsonResult.Error, "保存失败", null, null);
            }
        }

        //换人操作
        [HttpPost]
        public ActionResult CardInfoHR()
        {
            int id = Convert.ToInt32(Request.Form["id"]);
            //通过id取到原来油卡的全部信息
            C_OilCardInfo model = new C_OilCardInfoBLL().GetModelList(id);
            string sDetailInfo = Request.Form["sCardInfo"].ToString();
            C_OilCardInfo info = JsonHelper.GetJsonInfoBy<C_OilCardInfo>(sDetailInfo);
            //string centerID = new C_OilCardRechargeBLL().GetCenterID(model.CardCode);
            //string stationID = new C_OilCardRechargeBLL().GetStationID(model.CardCode);
            //info.CenterID = Convert.ToInt32(centerID);
            //info.StationID = stationID;
            info.CenterID = model.CenterID;
            info.StationID = model.StationID;
            //判断有无更改信息，如果无，则不做操作
            if (model.CardCode == info.CardCode && model.CardAmount == info.CardAmount && model.DriverName == info.DriverName && model.DriverWorkID == info.DriverWorkID)
            {               
                return this.JsonResult(Utility.E_JsonResult.OK, "保存成功！", null, null);
            }
            //如果有，则按换人操作流程走
            else
            {
                //先将原来卡的信息变为无效
                model.IsActive = false;
                //int res = new C_OilCardInfoBLL().Modify(model, "IsActive");
                //再新增换人后信息
                info.FillTime = DateTime.Now;
                info.LinkCardCode = model.LinkCardCode;
                info.bak1 = model.bak1;
                List<C_OilCardInfo> list = new C_OilCardInfoBLL().GetMaxID();
                if (list == null)
                { info.ID = 1; }
                else
                { info.ID = list[0].ID + 1; }
                int resh = new C_OilCardInfoBLL().Add(info);
                //换人操作备注
                model.Remark = model.Remark + "(换人操作,绑定人换成" + info.DriverName + "(" + info.DriverWorkID + "))";
                int res = new C_OilCardInfoBLL().Modify(model, "IsActive", "Remark");
                if (resh > 0)
                    return this.JsonResult(Utility.E_JsonResult.OK, "保存成功！", null, null);
                else
                    return this.JsonResult(Utility.E_JsonResult.Error, "保存失败", null, null);
            }
        }
        /// <summary>
        ///  注销/启用
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CardInfoDisable()
        {
            int id = Convert.ToInt32(Request.Form["id"]);
            bool disable = Convert.ToBoolean(Request.Form["disable"]);
            C_OilCardInfo model = new C_OilCardInfo();
            model.IsActive = disable;
            model.ID = id;
            int res = new C_OilCardInfoBLL().Modify(model, "IsActive");
            string tInfo = "";
            string finfo = "";
            if (disable)
            {
                tInfo = "“<span style='color:darkgreen'>启用</span>”成功！";
                finfo = "“<span style='color:darkgreen'>启用</span>”失败！";
            }
            else
            {
                tInfo = "“<span style='color:red'>注销</span>”成功！";
                finfo = "“<span style='color:red'>注销</span>”失败！";
            }
            if (res > 0)
                return this.JsonResult(Utility.E_JsonResult.OK, tInfo, null, null);
            else
                return this.JsonResult(Utility.E_JsonResult.Error, finfo, null, null);
        }

        // 获取卡号(所有的)      
        [HttpPost]
        public ActionResult GetCardCode(E_StatisticsPermisson em, string selfCenterID, string selfStationID, string selfWorkCode)
        {
            try
            {
                C_OilCardInfoBLL bll = new C_OilCardInfoBLL();
                var result = new object();
                //增加缓存
                //lock (m_SyncRoot)
                //{
                //    result = CacheHelper.GetCache("GetCardCode");
                //    if (result == null)
                //    {
                result = bll.GetCardCodeList(em, selfCenterID, selfStationID, selfWorkCode);
                //CacheHelper.SetCache("GetCardCode", result);
                //    }
                //}
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        // 获取卡号(取有效的)     
        [HttpPost]
        public ActionResult GetYXCardCode(E_StatisticsPermisson em, string selfCenterID, string selfStationID, string selfWorkCode)
        {
            try
            {
                C_OilCardInfoBLL bll = new C_OilCardInfoBLL();
                var result = new object();
                //增加缓存
                //lock (m_SyncRoot)
                //{
                //    result = CacheHelper.GetCache("GetYXCardCode");
                //    if (result == null)
                //    {
                result = bll.GetYXCardCodeList(em, selfCenterID, selfStationID, selfWorkCode);
                //        CacheHelper.SetCache("GetYXCardCode", result);
                //    }
                //}
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        // 获取卡号(所有的)判断卡号是否已经存在     
        [HttpPost]
        public ActionResult GetYXCardCodes()
        {
            C_OilCardInfoBLL bll = new C_OilCardInfoBLL();
            var result = new object();
            result = bll.GetYXCardCode();
            return Json(result);
        }
        // 获取绑定(司机)姓名     
        [HttpPost]
        public ActionResult GetDriverNames()
        {
            try
            {
                C_OilCardInfoBLL bll = new C_OilCardInfoBLL();
                var result = new object();
                //增加缓存
                lock (m_SyncRoot)
                {
                    result = CacheHelper.GetCache("GetDriverNames");
                    if (result == null)
                    {
                        result = bll.GetDriverNamesList();
                        CacheHelper.SetCache("GetDriverNames", result);
                    }
                }
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }

        // 获取绑定(司机)工号     
        [HttpPost]
        public ActionResult GetDriverWorkID(E_StatisticsPermisson em, string selfCenterID, string selfStationID, string selfWorkCode)
        {
            try
            {
                C_OilCardInfoBLL bll = new C_OilCardInfoBLL();
                var result = new object();
                //增加缓存
                //lock (m_SyncRoot)
                //{
                //    result = CacheHelper.GetCache("GetDriverWorkID");
                //    if (result == null)
                //    {
                result = bll.GetDriverWorkIDList(em, selfCenterID, selfStationID, selfWorkCode);
                //        CacheHelper.SetCache("GetDriverWorkID", result);
                //    }
                //}
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        // 获取负责人工号     
        [HttpPost]
        public ActionResult GetWorkID()
        {
            try
            {
                C_OilCardInfoBLL bll = new C_OilCardInfoBLL();
                var result = new object();
                //增加缓存
                //lock (m_SyncRoot)
                //{
                //    result = CacheHelper.GetCache("GetDriverWorkID");
                //    if (result == null)
                //    {
                result = bll.GetWorkIDList();
                //        CacheHelper.SetCache("GetDriverWorkID", result);
                //    }
                //}
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        // 根据绑定(司机)工号获取绑定(司机)姓名     
        [HttpPost]
        public ActionResult GetListByWorkID(string workID)
        {
            C_OilCardLink model = new C_OilCardInfoBLL().GetOilCardLinkList(workID);
            return Json(new { model }, "appliction/json", JsonRequestBehavior.AllowGet);
        }

        //public ActionResult GetDriverName( string workID)
        //{
        //    try
        //    {
        //        C_OilCardInfoBLL bll = new C_OilCardInfoBLL();
        //        var result = new object();
        //        //增加缓存
        //        //lock (m_SyncRoot)
        //        //{
        //        //    result = CacheHelper.GetCache("GetDriverName");
        //        //    if (result == null)
        //        //    {
        //                result = bll.GetDriverNameList(workID);
        //                CacheHelper.SetCache("GetDriverName", result);
        //            //}
        //        //}
        //        return Json(result);
        //    }
        //    catch (Exception)
        //    {
        //        return this.Json("");
        //    }
        #region  病历相关统计查询条件姓名和工号专用
        // 根据姓名获取对应工号     
        [HttpPost]
        public ActionResult GetWorkIDs(string name)
        {
            try
            {
                C_OilCardInfoBLL bll = new C_OilCardInfoBLL();
                var result = new object();
                result = bll.GetWorkIDLists(name);
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        //获取姓名     
        [HttpPost]
        public ActionResult GetNames()
        {
            try
            {
                C_OilCardInfoBLL bll = new C_OilCardInfoBLL();
                var result = new object();
                result = bll.GetNameList();
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        //获取工号    
        [HttpPost]
        public ActionResult GetWorkCode()
        {
            try
            {
                C_OilCardInfoBLL bll = new C_OilCardInfoBLL();
                var result = new object();
                result = bll.GetWorkCode();
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        #endregion

        // 根据绑定(司机)工号获取绑定(司机)姓名  
        [HttpPost]
        public ActionResult GetDriverName(string driverWorkID)
        {
            try
            {
                C_OilCardInfoBLL bll = new C_OilCardInfoBLL();
                var result = bll.GetDriverNameLists(driverWorkID);
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
        //取和当前选中卡有关的卡的信息列表
        [HttpPost]
        public ActionResult CardInfoLink()
        {
            try
            {
                //获取页容量
                int pageIndex = int.Parse(Request.Form["rows"]);
                //获取请求的页码
                int pageSize = int.Parse(Request.Form["page"]);
                //获取选中行的卡号和绑定工号
                string linkCardCode = Request.Form["LinkCardCode"].ToString();

                C_OilCardInfoBLL bll = new C_OilCardInfoBLL();
                var result = bll.GetLink(pageIndex, pageSize, linkCardCode);
                return Json(result);
            }
            catch (Exception)
            {
                return this.Json("");
            }
        }
    }
}
